﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections.ObjectModel;

namespace HtmlExtractor.Config
{
    public class GroupInfoCollection : Collection<GroupInfo>
    {

        public PageResult GetParseResult(SegmentMatchGroupCollection matchGroups,ParserContext context)
        {
            IEnumerable<SegmentMatchGroup> groups = matchGroups.Cast<SegmentMatchGroup>();
            PageResult pg = new PageResult();
            if (matchGroups.Count > 1)
            {
                //处理组合组
                CompositeItem cgi = new CompositeItem();
                foreach (GroupInfo g in Items)
                {
                    //多个组必须命名
                    SimpleItem gi = g.GetGroupResult(groups.Single(gm => gm.Name == g.Name),context);
                    if (!string.IsNullOrEmpty(gi.OutputPath))
                    {
                        
                        cgi.Add(gi);
                    }
                    if (g.SubRule != null)
                    {
                        pg.AddRange(g.GetSubResult(groups.Single(gm => gm.Name == g.Name),context));
                    }

                    //pg.AddRange(g.GetParsResult(groups.Single(gm => gm.Name == g.Name)));
                }
                pg.Add(cgi);
            }
            else
            {
                foreach (GroupInfo g in Items)
                {
                    //可以是匿名组
                    if (!string.IsNullOrEmpty(g.OutputPath))
                    {
                        PageResult tt = g.GetParsResult(groups.Single(), context);
                        if (tt != null)
                        {
                            pg.AddRange(tt);
                        }
                    }

                }
            }
            return pg;
        }
    }
}
